package zero;

import io.netty.channel.ServerChannel;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
/**
 * 基于NIO的数据传输 零拷贝数据传输
 * @author yunlongn
 */
public class NettyClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.connect(new InetSocketAddress("localhost", 8899));

        socketChannel.configureBlocking(true);

        String filePath = "D:\\book\\A从 0 开始带你成为MySQL实战优化高手.pdf";
        FileChannel fileChannel = new FileInputStream(filePath).getChannel();

        long currentTimeMillis = System.currentTimeMillis();
        long transferToIndex = 0;
        while (transferToIndex < fileChannel.size()) {
            transferToIndex += fileChannel.transferTo(transferToIndex, fileChannel.size(), socketChannel);
        }
        // 发送总字节数 88911673 耗时：226
        System.out.println("发送总字节数 " + transferToIndex + " 耗时：" +  (System.currentTimeMillis() - currentTimeMillis));

        socketChannel.close();
        fileChannel.close();
        Thread.sleep(1000000);
    }
}
